<?php
/**
 * Created by PhpStorm.
 * User: Janpoem
 * Date: 2015/3/10
 * Time: 5:18
 */

require __DIR__ . '/../autoload.php';

use Agi\Config\Db;

class TestDb extends PHPUnit_Framework_TestCase
{

    private $config1 = array(
        'adapter' => Db::MYSQL_NAT,
        'host'    => '127.0.0.1',
        'db'      => 'test_db',
        'prefix'  => 'test'
    );

    private $multiConfig = array(
        // default
        array(
            'adapter' => Db::MYSQL_PDO,
            'host'    => 'mysql-host',
            'port'    => 1234,
            'db'      => 'test_db',
            'prefix'  => 'test'
        ),
        // another
        'sqlsrv' => array(
            'adapter' => Db::SQLSRV_PDO,
            'host'    => 'sqlsrv-host',
            'db'      => 'TestDb',
            'port'    => 1173
        ),
    );

    public function testConfig1()
    {
        Db::define('config1', $this->config1);

        $conf = Db::get('config1');

        $this->assertEquals(Db::MYSQL_VALUE, $conf->type); // 是否是mysql？
        $this->assertEquals($this->config1['host'], $conf->host);
        $this->assertEquals(3306, $conf->port); // mysql 默认端口是否正确？
        $this->assertEquals(null, $conf->username);
        $this->assertEquals($this->config1['db'], $conf->db);
        $this->assertEquals($this->config1['prefix'], $conf->prefix);
        $this->assertEquals(false, $conf->isPdo); // 是不是不是PDO的驱动？
        $this->assertEquals(true, $conf->isNative); // 是否原生驱动
    }

    public function testMulti()
    {
        Db::defineMulti($this->multiConfig);

        $default = Db::get();
        $sqlsrv = Db::get('sqlsrv');
        $config1 = Db::get('config1');

        // test object
        $this->assertNotEquals($default, Db::get('sqlsrv'));
        $this->assertNotEquals($default, Db::get('config1'));
        $this->assertEquals($default, Db::get());

        // default test
        $this->assertEquals($this->multiConfig[0]['port'], $default->port);
        $this->assertEquals($this->multiConfig[0]['host'], $default->host);
        $this->assertEquals(Db::MYSQL_NAME, $default->name);
        $this->assertEquals(Db::MYSQL_VALUE, $default->type);

        // sqlsrv test
        $this->assertEquals($this->multiConfig['sqlsrv']['port'], $sqlsrv->port);
        $this->assertEquals($this->multiConfig['sqlsrv']['host'], $sqlsrv->host);
        $this->assertEquals(Db::SQLSRV_NAME, $sqlsrv->name);
        $this->assertEquals(Db::SQLSRV_VALUE, $sqlsrv->type);


        $this->assertNotEquals(Db::SQLITE_NAME, $sqlsrv->name);
    }

    public function testRegister()
    {
        // 定义一些常量
        define('MYDB_NAME', 'my-db');
        define('MYDB_VALUE', 8);
        define('MYDB_PDO', 80 + Db::PDO_VALUE);
        // 注册一个数据库
        if (Db::register(MYDB_VALUE, MYDB_NAME)) {
            Db::define('my-db', array(
                'adapter' => MYDB_PDO,
                'host' => 123,
                'any' => '123',
            ));

            $conf = Db::get('my-db');

            $this->assertEquals(123, $conf->host);
            $this->assertEquals('123', $conf->any);
            $this->assertEquals(MYDB_NAME, $conf->name);
            $this->assertEquals(MYDB_VALUE, $conf->type);
            $this->assertEquals(true, $conf->isPdo);
        }
    }
}
